package com.sk.cardeal.act.home.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sk.cardeal.R;
import com.sk.cardeal.act.home.activity.ChoosePartsActivity;
import com.sk.cardeal.adapter.ModelAdapter;
import com.sk.cardeal.base.BaseFragment;
import com.sk.cardeal.callback.MyCallBack;
import com.sk.cardeal.entity.Model;
import com.sk.cardeal.entity.PartsListBean;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.MyUtils;
import com.sk.cardeal.view.TitleView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by sk on 2018/1/11.
 */

public class ChooseModelFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, CompoundButton.OnCheckedChangeListener {
    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    @BindView(R.id.kind_ryc)
    RadioGroup kindRyc;

    private ModelAdapter adapter;

    private MyCallBack myCallBack;

    Map<String, List<Model>> map;

    private List<String> keyList = new ArrayList<>();

    private List<Model> selectList = new ArrayList<>();

    private List<RadioButton> radioButtons = new ArrayList<>();

    public MyCallBack getMyCallBack() {
        return myCallBack;
    }

    public void setMyCallBack(MyCallBack myCallBack) {
        this.myCallBack = myCallBack;
    }

    @Override
    protected int getContentView() {
        return R.layout.layout_model;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        titleView.setMyCallBack(this);

        for (int i = 0 ; i < keyList.size() ; i++) {
            RadioButton radioButton = (RadioButton) LayoutInflater.from(mContext).inflate(R.layout.item_kind, kindRyc, false);
            radioButton.setTag(keyList.get(i));
            radioButton.setText(keyList.get(i));
            kindRyc.addView(radioButton);
            radioButtons.add(radioButton);
            if(i == 0){
                radioButton.setChecked(true);
            }
            radioButton.setOnCheckedChangeListener(this);
        }
        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new ModelAdapter(R.layout.item_model, selectList);
        dataRyc.setAdapter(adapter);
        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        adapter.setOnItemClickListener(this);

    }

    public void setData( Map<String, List<Model>> resultMap) {
        keyList.clear();
        map = resultMap;
        for (Map.Entry<String, List<Model>> entry : resultMap.entrySet()) {
            keyList.add(entry.getKey());
        }
        selectList = JSON.parseArray(JSON.toJSONString(map.get(keyList.get(0))), Model.class);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        myCallBack.selectModel(selectList.get(position));
    }

    @Override
    public void clickTitle(int i) {
        super.clickTitle(i);
        switch (i) {
            case 1:
                FragmentManager fm1 = getActivity().getSupportFragmentManager();
                FragmentTransaction ft1 = fm1.beginTransaction();
                ft1.remove(this);
                ft1.commit();
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            selectList.clear();
            String key = (String) buttonView.getText();
            selectList.clear();
            selectList.addAll(JSON.parseArray(JSON.toJSONString(map.get(key)), Model.class));
            Log.v("onCheckedChanged",selectList.size()+"");
            adapter.notifyDataSetChanged();
        }
    }
}
